// pages/used/usedDetail/usedDetail.js
const App = getApp();
const utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAgree:true,
    //确认购买弹窗
    ispwBoor:false,
    buy:false,
    //购买成功弹窗
    buySuccess:false,
    isClicked:true,
    isshow:2
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var id = options.id;
    var sourt = options.sourt;
    this.setData({
      id:options.id,
      sourt:sourt
    })
  },
  //是否同意协议
  isAgress:function (){
    let isAgress = this.data.isAgree;
    if (isAgress) {
      this.setData({
        isAgree:false
      })
    }else{
      this.setData({
        isAgree:true
      })
    }
  },

  getDetailData:function (markId,id){
    utils.requestUrl({//将用户信息传给后台数据库
      // method:'post',
      url: "/niu/apiv3/xiandan_getdetail.php",
      params: {
        markid: markId,
        xiandanid:id
      }
    }).then((res) => {
      let data = res.data;
      console.log(data);
      let outtime = data.outtime.substring(0,4)+'年'+data.outtime.substring(4,6) + '月' + data.outtime.substring(6,8) + '日';
      let time = data.toys.book_time.substring(0,4)+'年'+data.toys.book_time.substring(4,6) + '月';
      var book_time = '';
      if (data.toys.book_time == 0){
        book_time = '无'
      }else{
        book_time = time;
      }
      var bkmoneyrmb;
      var moneyrmbQK;
      if (data.bukuan_status == 0){
        bkmoneyrmb = Math.ceil(Number(data.bukuan_money2)/6)
        moneyrmbQK = Math.ceil((Number(data.bukuan_money2)+Number(data.dingjin_money2))/6)
      }
      this.setData({
        ispw:data.ispw,//是否为密码交易
        userId:data.userid,
        isshow:data.isshow,
        cuxiaoid:data.cuxiaoid,
        moneyrmb:data.moneyrmb,
        bkmoneyrmb:bkmoneyrmb,
        moneyrmbQK:moneyrmbQK,
        bukuan_status:data.bukuan_status,
        userface:data.userface,
        toyspic:data.toyspic,
        toysname:data.toysname,
        usernick:data.usernick,
        uservip:data.uservip,
        intro:data.intro,
        outtime:outtime,
        book_time:book_time,
        introInfo:data.toys.introinfo,
        choumoney2:data.backmoneyhp,
        price:data.toys.price,
        mainseries:data.toys.mainseries,
        sptype:data.toys.sptype,
        maker:data.toys.maker,
        secondseries:data.toys.secondseries
      })
    })
  },
  //联系卖家
  goToChat:function () {
    if(wx.getStorageSync('markId') == ''){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }else{
      var userid = this.data.userId;
      var id = this.data.id;
      var toyspic = this.data.toyspic;
      var title = this.data.toysname;
      var price = (this.data.moneyrmb) / 100;
      var name = 'djuser_'+userid;
      if (userid == wx.getStorageSync('userId')){
        utils.toast('不能与自己联系')
      }else{
        wx.navigateTo({
          url: '/pages/message/chat/chat?name='+name+'&id='+id+'&title='+title+'&price='+price+'&toyspic='+toyspic,
        })
      }
    }
  },
  // 购买
  bugBtn:function (){
    
    if(wx.getStorageSync('markId') == ''){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }else{
      this.setData({
        buy:true
      })
    }
  },
  //取消购买
  cancel:function (){
    this.setData({
      buy:false
    })
  },
  //去个人中心
  goToIndex:function (){
    let id = this.data.userId
    wx.navigateTo({
      url: '/pages/my/myIndex/myIndex?userId='+id,
    })
  },
  bindinput:function (e){
    let code = e.detail.value;
    console.log(code)
    this.setData({
      code:code
    })
  },
  //隐藏输入密码
  cancelBuy:function () {
    this.setData({
      ispwBoor:false
    })
  },
  selectPay:function (e){
    var selectPay = e.currentTarget.dataset.index;
    var payType;
    console.log(selectPay);
    if (selectPay == 0){
      payType = 0
    }else if(selectPay == 1){
      payType = 1
    }
    this.setData({
      payType:payType
    })
  },
  getMoney:function(){
    utils.requestUrl({
      url:'/niu/apiv3/xiandanv3_mymoneyrmb.php?method=total',
      params: {
        markid:wx.getStorageSync('markId')
      }
    }).then((res) =>{
      console.log(res.data);
      let data = res.data.data;
      let balance = data.cannowout;
      var payType;
      if (balance > 0){
        payType = 0;
      }else{
        payType = 1
      }
      this.setData({
        balance:balance,
        payType:payType
      })
    })
  },
  //密码交易购买
  passwordBuy:function () {
    var that = this;
    if (this.data.code == '') {
      utils.toast('密码不能为空')
      return
    }
    utils.requestUrl({
      url: "/niu/apiv3/xiandanv3_docheckcode.php?method=checkcode",
      params: {
        markid: wx.getStorageSync('markId'),
        xiandanid: this.data.id,
        code:this.data.code
      }
    }).then((res) => {
      var data = res.data;
      console.log(data);
      var payType = that.data.payType;
      if(data.resultcode == 0){
        this.setData({
          ispwBoor:false
        })
        if (payType == 1){
          that.wxPay('xiandan');
        }else if(payType == 0){
          var balance = that.data.balance;
          var moneyrmb = that.data.moneyrmb;
          var id = that.data.id
          if(balance < moneyrmb){
            that.wxPay('partpay_xiandan');
          }else{
            utils.requestUrl({
              url: "/niu/apiv3/xiandanv3_dobuyrmbv2.php?method=buy",
              params: {
                markid: wx.getStorageSync('markId'),
                xiandanid:id
              }
            }).then((res) => {
              var data = res.data;
              console.log(data);
              if(data.resultcode == 0){
                that.setData({
                  buy:false,
                  buySuccess:true
                })
              }else{
                utils.toast(data.msg)
              }
            })
          }
        }
      }else{
        utils.toast(data.msg)
      }
    })
  },
  //确认购买 (唤起微信支付)(咸蛋商品)
  affirmBuy:function (){
    let that = this;
    var isClicked = that.data.isClicked;
    console.log(isClicked)
    if (isClicked){
      that.setData({
        isClicked:false
      })
      let isAgree = this.data.isAgree;
      var payType = this.data.payType;
      if (!isAgree) {
        utils.toast('请同意蛋酱咸蛋交易协议')
        return
      }
      let ispw = this.data.ispw;
      //判断是否为密码交易
      if (ispw == 1) {
        this.setData({
          ispwBoor:true,
          isClicked:true
        })
      }else{
        if (payType == 1){
          that.wxPay('xiandan');
        }else if(payType == 0){
          var balance = that.data.balance;
          var moneyrmb = that.data.moneyrmb;
          var id = that.data.id
          if(balance < moneyrmb){
            that.wxPay('partpay_xiandan');
          }else{
            utils.requestUrl({
              url: "/niu/apiv3/xiandanv3_dobuyrmbv2.php?method=buy",
              params: {
                markid: wx.getStorageSync('markId'),
                xiandanid:id
              }
            }).then((res) => {
              var data = res.data;
              console.log(data);
              if(data.resultcode == 0){
                that.setData({
                  isClicked:true,
                  buy:false,
                  buySuccess:true
                })
              }else{
                that.setData({
                  isClicked:true
                })
                utils.toast(data.msg)
              }
            })
          }
        }
      }
    }
  },
  wxPay:function (paytype) {
    let that = this;
    utils.requestUrl({
      url: "/niu/wxpay/wxprepay_wxsapp.php?paytype="+paytype,
      params: {
        markid: wx.getStorageSync('markId'),
        payforid: this.data.id,
        openid: wx.getStorageSync('openId')
      }
    }).then((res) => {
      var data = res.data;
      console.log(data);
      wx.requestPayment({
        timeStamp: data.timeStamp,
        nonceStr: data.nonceStr,
        package: data.package,
        signType: 'MD5',
        paySign: data.paySign,
        success (res) {
          console.log(res)
          if(res.errMsg == 'requestPayment:ok'){
            that.setData({
              buy:false,
              isClicked:true,
              buySuccess:true
            })
          }
        },
        fail (res) {
          console.log(res)
          that.setData({
            isClicked:true,
          })
          utils.toast('支付失败,请重新支付')
        }
      })
    })
    
  },
  //关闭购买弹窗
  close:function () {
    var sourt = this.data.sourt
    wx.switchTab({
      url:'/pages/used/used'
    })
    // if(sourt == 'app'){
    //   wx.switchTab({
    //     url:'/pages/used/used'
    //   })
    // }else{
    //   wx.navigateBack({
    //     delta:1
    //   })
    // }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //判断是否登录
    // if(wx.getStorageSync('markId') == ''){
    //   wx.navigateTo({
    //     url: '/pages/login/login',
    //   })
    // }
    let markId = wx.getStorageSync('markId')
    let id = this.data.id
    this.getDetailData(markId,id);
    this.getMoney();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})